package com.xiyue.server.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.sport.common.web.controller.BaseController;
import com.sport.common.web.model.PageParams;
import com.sport.common.web.model.Result;
import com.sport.model.entity.Food;
import com.xiyue.server.annotation.RestControllerMapping;
import com.xiyue.server.service.FoodService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

 /**
 * 食物信息;(food)表控制层
 * @author: xiyue
 * @date : 2025-8-14
 */
@Tag(name = "食物信息")
@RestControllerMapping("/food")
@ApiSupport(author = "xiyue:chencankun.dev@foxmail.com", order = 9)
public class FoodController extends BaseController {

    @Resource
    private FoodService foodService;
    
    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<Food>> pageQuery(@RequestBody PageParams<Food> pageParams) {
        // 1.查询参数
        Food params = pageParams.getParams();
        LambdaQueryWrapper<Food> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.eq(params.getTitle() != null, Food::getTitle, params.getTitle());
        lqw.eq(params.getTypeId() != null, Food::getTypeId, params.getTypeId());
        // 3.分页查询
        return Result.success(foodService.page(pageParams.getPage(), lqw));
    }
    
    @PostMapping("insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据")
    @SaCheckPermission("food:type:save")
    public Result<Boolean> insert(@RequestBody Food food) {
        return toResult(foodService.save(food));
    }
    
    @PostMapping("update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@RequestBody Food food) {
        return toResult(foodService.updateById(food));
    }
    
    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据",
            parameters = {
            @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id){
        return toResult(foodService.removeById(id));
    }
}